//
//  UserCareerAwardCell.m
//  CoolTennisBall
//
//  Created by Coollang on 16/8/31.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "UserCareerAwardCell.h"
#import "UIView+Frame.h"
#import "UIImageView+CM_ShowImage.h"
#import "CoollangTennisBall-Swift.h"
#import "UserHomeModel.h"

@interface UserCareerAwardCell ()<UIScrollViewDelegate>

@property (weak, nonatomic)IBOutlet UIPageControl *pageControl;
@property (weak, nonatomic)IBOutlet UIScrollView *awardScrollView;

@property (strong, nonatomic) IBOutletCollection(UIImageView) NSArray *medalIcons;

@end

@implementation UserCareerAwardCell

- (IBAction)pageChange:(UIPageControl *)sender {
    
    NSInteger page = sender.currentPage;
    [self.awardScrollView setContentOffset:CGPointMake(page * CGRectGetWidth(self.bounds), 0) animated:YES];
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateUserAward) name:@"UpDataUserCareerData" object:nil];
        
    }
    
    return self;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)updateUserAward {
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.15 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self showMedalIconWithMedalItems:self.userModel.medalItems];
    });
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.awardScrollView.delegate = self;
    [self configureDefaultMedalIcons];
    [self medalIconsAddGestureRecognizer];
}

#pragma mark - Private Method
- (void)medalIconsAddGestureRecognizer
{
    for (UIImageView *medalIcon in self.medalIcons) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showMedalIcon:)];
        [medalIcon addGestureRecognizer:tap];
        medalIcon.userInteractionEnabled = YES;
    }
}
- (void)showMedalIcon:(UITapGestureRecognizer *)gestureRecognizer {
    if (![NSBundle currentLanguageIsChinese]) {
        return;
    }
    
    NSInteger medalIndex = gestureRecognizer.view.tag - 100;
    if (medalIndex < [self.userModel.medalItems count]) {
       // NSLog(@"%zd %zd", medalIndex, [self.userModel.medalItems count]);
        MedalItem *item = self.userModel.medalItems[medalIndex];
        [self showMedalIconWithFirstStateMedalIndex:item.typeIndex];
    }
}

- (BOOL)hasMedalIconWithIndex:(NSUInteger)index {
    
    for (MedalItem *item in self.userModel.medalItems) {
        if (index == item.typeIndex) {
            return YES;
        }
    }
    return NO;
}
- (void)configureDefaultMedalIcons {
    for (UIImageView *medalIcon in self.medalIcons) {
        if (medalIcon.tag - 100 < 6) {
            medalIcon.image = [UIImage imageNamed: [NSString stringWithFormat:NSLocalizedString(@"medal_icon_%d_en", nil), medalIcon.tag - 100]];
        }
    }
}
- (void)showMedalIconWithMedalItems:(NSArray *)medalItems
{
    NSLog(@"%@", medalItems);
    NSMutableArray *medalIconIndexs = @[].mutableCopy;
    for (NSInteger index = 0; index < [self.medalIcons count]; index++) {
        [medalIconIndexs addObject:@(index)];
    }
    
    for (NSInteger index = 0; index < [medalItems count]; index++) {
        UIImageView *medalIcon = [self medalIconWithTag:index];
        MedalItem *item = medalItems[index];
        medalIcon.image = [UIImage imageNamed: [NSString stringWithFormat:NSLocalizedString(@"medal_icon_get_%d_en", nil), item.typeIndex]];
        if ([NSBundle currentLanguageIsChinese]) {
            if (item.firstGet) {
                [self showMedalIconWithFirstStateMedalIndex:item.typeIndex];
            }
        }
        
        if([medalIconIndexs containsObject:@(item.typeIndex)]) {
            [medalIconIndexs removeObject:@(item.typeIndex)];
        }
    }
    
    for (NSInteger index = [medalItems count]; index < [self.medalIcons count]; index++) {
        UIImageView *medalIcon = [self medalIconWithTag:index];
        NSInteger medalIndex = [medalIconIndexs[index - [medalItems count]] integerValue];
        // 隐藏icon需要注意是否显示默认
        NSInteger hideMedalIconIndex = [self.medalIcons count] - 3;
        if (index < hideMedalIconIndex|| (index >= hideMedalIconIndex && medalIndex < hideMedalIconIndex)) {
            medalIcon.image = [UIImage imageNamed: [NSString stringWithFormat:NSLocalizedString(@"medal_icon_%d_en", nil), medalIndex]];
        }
    }
}

- (UIImageView *)medalIconWithTag:(NSInteger)tag {
    
    for (UIImageView *medalIcon in self.medalIcons) {
        if (medalIcon.tag - 100 == tag) {
            return medalIcon;
        }
    }
    return nil;
}

- (void)showMedalIconWithFirstStateMedalIndex:(NSInteger)index
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"UserCenter" bundle:nil];
    MedalViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"MedalViewController"];
    controller.medalIndex = index;
    [controller presentInParentViewController:self.targetVc.tabBarController];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger page =  scrollView.contentOffset.x / CGRectGetWidth(self.bounds);
    self.pageControl.currentPage = page;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.width =kYFScreenWidth;
}

@end
